package tests;

import processing.core.*;

import java.util.*;

import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.geo.Location;
import de.fhpotsdam.unfolding.utils.MapUtils;
import de.fhpotsdam.unfolding.providers.*;
import de.fhpotsdam.unfolding.marker.*;
import de.fhpotsdam.unfolding.data.*;
import de.fhpotsdam.unfolding.marker.SimplePointMarker;

@SuppressWarnings("serial")
public class simpleMap_geoJSON_marker extends PApplet {
	
	int appWidth = 1024;
	int appHeight = 768;
	
	//GLOBAL FILE URLS
	String iExpresStations_URL = "../DataAssets/CKW_IExpressStations_wgs84.geojson";
	String iExpressLine_URL = "../DataAssets/CKW_IExpressRoute_wgs84.geojson";
	String iExpressSheds_URL = "../DataAssets/CKW_IExpressSheds_wgs84.geojson";
	 
    UnfoldingMap map;
    
    // declare maps for a few default providers
    UnfoldingMap msAerial;
    UnfoldingMap msRoad;
    UnfoldingMap stamenToner;
    UnfoldingMap osmGray;
    
    Location kcw = new Location(43.42f, -80.41f); // kcw (latitude, longitude) 
	
    float maxPanningDistance = 15;
    
    ArrayList<Marker> stationMarkers = new ArrayList<Marker>(); 
    
    public void setup() {
		size(1024, 768, P2D);
		
		// assign a few default map providers
		//msAerial = new UnfoldingMap(this, new Microsoft.AerialProvider());
		//msRoad = new UnfoldingMap(this, new Microsoft.RoadProvider());
		stamenToner = new UnfoldingMap(this, new StamenMapProvider.Toner());
		//osmGray = new UnfoldingMap(this, new OpenStreetMap.OSMGrayProvider());
		
		map = stamenToner; // set map provider
		
		map.zoomAndPanTo(kcw, 12); // set map starting location and zoom level
		map.setPanningRestriction(kcw, maxPanningDistance); 
		map.setZoomRange(11, 19);
		
		MapUtils.createDefaultEventDispatcher(this, map); // creates default pan & zoom -- zoom in w/mouse wheel or dbl-click, out with "-"

		
////////LOAD & DISPLAY SHED POLYGON //////// 
		
		 // load data
		 List<Feature> iExpressSheds = GeoJSONReader.loadData(this, iExpressSheds_URL);	 
		 List<Marker> iExpressShedMarkers = MapUtils.createSimpleMarkers(iExpressSheds);
		 
		 //style polygon
		 for (Marker shedMarker : iExpressShedMarkers) {
			 shedMarker.setColor(color(0, 200, 0, 20));
		 }
		 
		 // add markers to map
		 map.addMarkers(iExpressShedMarkers);
		 println("Plotting iExpress Sheds");
		 
////////END SHED POLYGON ////////		
				
		 
//////// LOAD & DISPLAY IEXPRESS LINE - GEOJSON ////////
		 
		 // load data
		 List<Feature> iExpressLine = GeoJSONReader.loadData(this, iExpressLine_URL); 
		 List<Marker> iExpressLineMarkers = MapUtils.createSimpleMarkers(iExpressLine);	 
		 
		// Create markers from features
		 for (Feature feature : iExpressLine) {
		     ShapeFeature lineFeature = (ShapeFeature) feature;
		  
		     // we could extend this in its own class to add style, as done with StationMarker class below
		     SimpleLinesMarker m = new SimpleLinesMarker(lineFeature.getLocations());
		     int lineColor = color(44, 91, 167);
		    		 
		     m.setColor(lineColor);
		     m.setStrokeWeight(5);
		     iExpressLineMarkers.add(m);
		    
		 }
		 
		 // add markers to map
		 map.addMarkers(iExpressLineMarkers);
		 println("Plotting iExpress Line");

//////// END IEXPRESS LINE - GEOJSON ////////						
	

//////// LOAD & DISPLAY STATION POINTS - GEOJSON //////// 
		 
		List<Feature> stations = GeoJSONReader.loadData(this, iExpresStations_URL);
		List<Marker> stationMarkers = MapUtils.createSimpleMarkers(stations);
		
		//List<Marker> markers = createLabeledMarkers(stations);
		//map.addMarkers(markers);
		
		// Create markers from features
		for (Feature feature : stations) {
		     PointFeature pointFeature = (PointFeature) feature;

		     //create position marker
		     StationMarker stnMarker = new StationMarker(pointFeature.getLocation());  
		     
		     //add to list
		     stationMarkers.add(stnMarker);
		     println("Plotting Station Marker @ " + stnMarker.getLocation());
		    
		 } 
		
		// add markers to map
		map.addMarkers(stationMarkers);
		
////////END STATION POINTS - GEOJSON ////////
		
		
    } // end setup

    
    
    
    public void draw() {
    	
        map.draw();
    
    } // end draw
   
    /*
	public void mouseMoved() {
		// Deselect all marker
		for (Marker marker : map.getMarkers()) {
			marker.setSelected(false);
		}

		// Select hit marker
		Marker marker = map.getFirstHitMarker(mouseX, mouseY);
		// NB: Use mm.getHitMarkers(x, y) for multi-selection.
		if (marker != null) {
			marker.setSelected(true);
		}
	}

	public List<Marker> createLabeledMarkers(List<Feature> features) {
		PFont font = loadFont("ui/OpenSans-12.vlw");
		List<Marker> markers = new ArrayList<Marker>();
		for (Feature feature : features) {
			String label = feature.getStringProperty("Name");
			PointFeature pointFeature = (PointFeature) feature;
			Marker marker = new LabeledMarker(pointFeature.getLocation(), label, font, 15);
			markers.add(marker);
		}
		return markers;
	}
*/
    
    
	//include to run as java application instead of applet
	public static void main(String args[]) {
		PApplet.main(new String[] { "--present", "tests.simpleMap_geoJSON_marker" });
	}   
    
//////////////// CLASSES ////////////////
   
    // station marker visualization
    public class StationMarker extends SimplePointMarker {
    	 
    	  public StationMarker(Location location) {
    	    super(location);
    	  }
    	 
    	  public void draw(PGraphics pg, float x, float y) {
    	    pg.pushStyle();
    	    pg.strokeWeight(3);
    	    pg.stroke (255,0,127);
    	    //pg.noFill();
    	    pg.fill(255,255,255);
    	    pg.ellipse(x, y, 12, 12);
    	    pg.popStyle();
    	  }
    	}
    


} // end class



/////// notes ////////

/*/////// LOAD & DISPLAY STATION POINTS - CSV //////// 

//String stationsCSV_URL = "../DataAssets/stationLocations.csv"; // not used

// load stations csv data into table
Table stationtable = loadTable(stationsCSV_URL,"header");

// read stations table data
for (TableRow row : stationtable.rows()) {
	    
	String id = row.getString("ID"); 
    float lon = row.getFloat("LONG");
    float lat = row.getFloat("LAT");
    String stationName = row.getString("NAME");
    String type = row.getString("TYPE");
    Location location = new Location(lat, lon);
    
    println("id: " + id + ", station: " + stationName + ", longitude: " + lon + ", latitude: " + lat + ", type: " + type);
    
    //create position marker
    StationMarker stnMarker = new StationMarker(location);	 
    
    //add to list
    stationMarkers.add(stnMarker);
    println("Plotting Station Marker @ " + stnMarker.getLocation());
    
} 

// add markers to map
map.addMarkers(stationMarkers);

////////END STATION POINTS - CSV ///////*/